package com.vancouver.knows.business.tracker;

import java.util.HashMap;
import java.util.List;

import com.pangu.bean.PageInfo;
import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.DiscountCategoryInfo;
import com.vancouver.knows.business.bean.HomeGridInfo;
import com.vancouver.knows.business.bean.HomeLiveFeedInfo;
import com.vancouver.knows.business.bean.HomeMenuInfo;
import com.vancouver.knows.business.bean.HomePageInfo;
import com.vancouver.knows.business.bean.WeatherInfo;
import com.vancouver.knows.business.service.impl.HomePageHandle;
import com.vancouver.knows.business.task.mark.HomePageTaskMark;

/**
 * 获取首页信息
 * 
 * @author llf
 * 
 */
public class HomePageTracker extends ABInvokeTracker {

	public HomePageTracker(BusinessModule module, IResultReceiver resultReceiver) {
		super(module, resultReceiver);
	}

	@Override
	protected void handleTaskRefresh(ATaskMark taskMark, OperateResult result) {
	}

	@Override
	public String TAG() {
		return HomePageTracker.class.getSimpleName();
	}

	@SuppressWarnings("unchecked")
	@Override
	public void handleResult(OperateResult result) {
		HashMap<String, Object> map = (HashMap<String, Object>) result.getResultData();
		if (map == null){
			return;
		}
		
		int from = (int) map.get("from");
		int size = (int) map.get("size");
		HomePageTaskMark homePageTaskMark = (HomePageTaskMark) result.getTaskMark();
		PageInfo pageInfo = homePageTaskMark.getPageInfo();
		pageInfo.setFrom(from);
		pageInfo.setSize(size);
		
		List<HomePageInfo> homepageInfo = (List<HomePageInfo>) map.get(HomePageHandle.HOMEPAGE_MAP);
		if(homepageInfo!=null){
			module.getCacheManager().setHomePageMap(map);
			List<DiscountCategoryInfo> discountCategoryInfoCache = (List<DiscountCategoryInfo>) map
					.get(HomePageHandle.HOMEPAGE_BUSINESS_CATEGORIES);
			module.getCacheManager().setDiscountCategoryInfoCache(discountCategoryInfoCache);

			List<HomeMenuInfo> homeMenuInfo = (List<HomeMenuInfo>) map
					.get(HomePageHandle.HOMEPAGE_MENU);
			module.getCacheManager().setHomeMenuInfoCache(homeMenuInfo);

			List<HomeGridInfo> homeGridInfo = (List<HomeGridInfo>) map
					.get(HomePageHandle.HOMEPAGE_GRID);
			module.getCacheManager().setHomeGridInfoCache(homeGridInfo);

			List<HomeLiveFeedInfo> homeLiveFeedInfos = (List<HomeLiveFeedInfo>) map
					.get(HomePageHandle.HOMEPAGE_LIVE_FEED);
			module.getCacheManager().setHomeLiveFeedInfoCache(homeLiveFeedInfos);

			WeatherInfo weatherInfo = (WeatherInfo) map.get(HomePageHandle.HOMEPAGE_WEATHER);
			module.getCacheManager().setHomeWeatherInfoCache(weatherInfo);
			
			//module.getCacheManager().getHomepageInfoCache().addItemInfoToCache(homePageTaskMark, homepageInfo);
			//List<HomePageCarouseInfo> homepageCarouseInfo = (List<HomePageCarouseInfo>) map.get(HomePageHandle.HOMEPAGE_CAROUS);
			//module.getCacheManager().getHomepageCarouseInfoCache().addItemInfoToCache(homePageTaskMark, homepageCarouseInfo);
			pageInfo.setTotalCount(homepageInfo.size());
		}
		
		homePageTaskMark.setPageInfo(pageInfo);
	}
}
